/** 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.free.i.virtual.machine.hal.devicealias;

import it.free.i.Factory;
import it.free.i.SystemFactory;
import it.free.i.naming.JndiPropertiesLoader;
import it.free.i.naming.NamingBuilder;
import it.free.i.virtual.machine.VirtualMachineFactory;
import it.free.i.virtual.machine.tools.ObjectIniector;

import java.util.Properties;

/**  
 * Virtual machine implementation factory
 * @author stefano
 */
public class DeviceAliasFactory implements Factory {
	
	public static String CONTEXT = null;
	private static final String MY_CONTEXT = "cn=DeviceAlias";
	public static final String DEVICES_TREE_JNDI_NAME = "cn=Devices";
	
	static{
		CONTEXT = NamingBuilder.buildLookupName(MY_CONTEXT, VirtualMachineFactory.CONTEXT);
	}
	
	/**
	 * No need to make possible to create instances
	 * Singleton makes it private, no creation from outside
	 */
	public DeviceAliasFactory(){
		super();
	}
	
	/**
	 * Create a device alias command by its FQ name
	 * @param lookupName is the name to lookup
	 * @return an instance of a generic object to cast to device alias command
	 * @throws Exception
	 */
	public Object createObject(String lookupName) throws Exception {
		
		Properties properties = new Properties();
		
		NamingBuilder.buildLookupName(lookupName, CONTEXT, SystemFactory.CONTEXT);
		
		properties = JndiPropertiesLoader.lookupProperties(lookupName);
		String className = properties.getProperty(DeviceAlias.IMPL_CLASS_PROP_NAME);
		DeviceAlias deviceAlias = (DeviceAlias) ObjectIniector.classForName(className); 
		
		deviceAlias.link(properties);
		
		return deviceAlias;
	}
}
